'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    ------
    
    About:
'''

import sys, types

import masdevallia.utility.Dag as Dag

def str_to_class( field ):
    '''
    Converts a string to a class.
    @param field: String. Name of a class.
    @return: Instance of class.
    '''
    try:
        identifier = getattr( sys.modules[__name__], field )
        
    except AttributeError:
        raise NameError( '{0} doesn\'t exist.'.format( field ) )
    
    if isinstance( identifier, ( types.ClassType, types.TypeType ) ):
        return identifier
    
    raise TypeError( '{0} is not a class.'.format( field ) )

def getMetaNode( name ):
    '''
    Checks if a MetaNode exists.
    
    @param name: String. Name of the MetaNode.
    @return: MetaNode or None
    '''
    print name
    if Dag.objExists( name ):
        print True
        # Get the MetaNode as a MObject.
        obj = Dag.getMObject( name )
        
        # Get its name attribute value.
        name_plug = Dag.getPlugByName( obj, 'metaName' )
        node_name = Dag.getPlugValue( name_plug )
        print node_name
        
        # Get its type attribute value.
        type_plug = Dag.getPlugByName( obj, 'metaType' )
        node_type = Dag.getPlugValue( type_plug )
        print node_type
        
        # Instance the class object to the passed in name.
        to_class = str_to_class( node_type )
        node = to_class( node_name=node_name )
        
        return node
    else:
        return None